package com.kevin.websocket.server.controller;

import com.kevin.websocket.server.configuration.websocket.WebSocketEndpoint;
import com.kevin.websocket.server.model.dto.MessageDTO;
import com.kevin.websocket.server.model.enums.PublishType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@Tag(name = "Websocket服务端")
@RequestMapping("/websocket/server")
public class WebsocketController {

    @Resource
    private WebSocketEndpoint webSocketEndpoint;

    @Operation(summary = "发布消息", description = "发布消息")
    @PostMapping("/publish")
    public void publish(@RequestBody MessageDTO messageDTO) {
        log.debug("server publish message : {}", messageDTO);
        String publishType = messageDTO.getPublishType();
        switch (PublishType.valueOfVal(publishType)) {
            case BROADCAST:
                webSocketEndpoint.broadcast(messageDTO);
                break;
            case MULTICAST:
                webSocketEndpoint.multicast(messageDTO);
                break;
            case UNICAST:
                webSocketEndpoint.unicast(messageDTO);
                break;
        }
    }

}
